package cn.globalph.housekeeper.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import java.io.File;

public class MapUtils {
    //传入坐标的坐标系为bd09（百度坐标系）
    //优先使用百度地图 然后高德地图 然后腾讯地图

    //map app包名
    private static final String BAIDU_MAP_APP = "com.baidu.BaiduMap";
    private static final String GAODE_MAP_APP = "com.autonavi.minimap";

    //调起导航的uri
    //baidumap://map/marker?location=40.057406655722,116.2964407172&title=Marker&content=makeamarker&traffic=on&src=andr.baidu.openAPIdemo
    private static final String BAIDU_MAP_NAVI_URI = "baidumap://map/marker?";
    //androidamap://viewMap?sourceApplication=appname&poiname=abc&lat=36.2&lon=116.1&dev=0
    private static final String GAODE_MAP_NAVI_URI = "androidamap://viewMap?";

    /**
     * 地图坐标系为百度坐标系
     *
     * @param lng 经度
     * @param lat 纬度
     */
    public static void showPoint(Context context, double lng, double lat, String title, String des) {
        try {
            if (isApplicationInstall(BAIDU_MAP_APP)) {
                //安装了百度map
                Intent intent = new Intent();
                intent.setData(Uri.parse(
                        BAIDU_MAP_NAVI_URI +
                                "location=" + lat + "," + lng
                                + "&title=" + title
                                + "&content=" + des
                                + "&traffic=on&src=cn.globalph.housekeeper"
                ));
                context.startActivity(intent);

            } else if (isApplicationInstall(GAODE_MAP_APP)) {
                //安装了高德map
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                double[] temp = CoordinateTransformUtil.bd09togcj02(lng, lat);
                //将功能Scheme以URI的方式传入data
                Uri uri = Uri.parse(GAODE_MAP_NAVI_URI +
                        "sourceApplication=cn.globalph.housekeeper"
                        + "&poiname" + title
                        + "&lat=" + temp[1]
                        + "&lon=" + temp[0]
                        + "&dev=" + 0);
                intent.setData(uri);
                intent.setPackage("com.autonavi.minimap");
                context.startActivity(intent);
            } else {
                SystemUtils.TT(context, "请安装百度地图或高德地图");
            }
        } catch (Exception e) {
            e.printStackTrace();
            SystemUtils.TT(context, "启动地图失败");
        }
    }

    private static boolean isApplicationInstall(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }


}
